package durak.client.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.border.LineBorder;

import durak.common.model.CommonPlayerData;
import durak.common.model.GameTableState;

public class MainDurakFrame extends JFrame {

	private HandView handView;
	private ActionListener passAttackListener;
	private ActionListener attackListener;
	private ActionListener withdrawListener;
	private DurakPanel scoreBoard;

	private DurakPanel messagesFromServer;
	private JButton btnPassattack;
	private JButton btnAttack;
	private JButton btnWithdraw;
	private GamingTableView gamingTableView;
	private ImageIcon backgroundImage;
	private DurakPanel buttonPanel;
	private JLabel playerNameLable;
	private JLabel playerScoreLable;
	private JLabel scoreBoardHeader;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmExit;
	private JMenu helpMenu;
	private JMenuItem about;

	/**
	 * @return the gamingTableView
	 */
	public GamingTableView getGamingTableView() {
		return gamingTableView;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainDurakFrame frame = new MainDurakFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * @param passAttackListener
	 *            the passAttackListener to set
	 */
	public void setPassAttackListener(ActionListener passAttackListener) {
		this.passAttackListener = passAttackListener;
		btnPassattack.addActionListener(passAttackListener);
	}

	/**
	 * @param attackListener
	 *            the attackListener to set
	 */
	public void setAttackListener(ActionListener attackListener) {
		this.attackListener = attackListener;
		btnAttack.addActionListener(attackListener);
	}

	/**
	 * Create the frame.
	 */
	public MainDurakFrame() {
		setTitle("The Best Durak Ever (by Miri and Alex)");
		URL fileLocation = LoginToDurak.class.getResource("/durak/resources/images/DURAK_ICON.png");
		setIconImage(new ImageIcon(fileLocation).getImage());
		GridBagConstraints con = new GridBagConstraints();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setSize(640, 480);
		setMinimumSize(new Dimension(1024, 728));

		con.fill = GridBagConstraints.SOUTH;
		getContentPane().setLayout(new BorderLayout(0, 0));

		messagesFromServer = new DurakPanel();
		messagesFromServer.setBorder(new LineBorder(Color.YELLOW, 1, true));
		messagesFromServer.setMinimumSize(new Dimension(200, 30));
		messagesFromServer.setBounds(new Rectangle(0, 0, 200, 30));
		messagesFromServer.setSize(200, 30);
		messagesFromServer.setMaximumSize(new Dimension(200, 30));
		messagesFromServer.setPreferredSize(new Dimension(200, 30));
		messagesFromServer.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 15));
		getContentPane().add(messagesFromServer, BorderLayout.NORTH);

		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnNewMenu = new JMenu("File");
		menuBar.add(mnNewMenu);

		mntmExit = new JMenuItem("Exit");
		mntmExit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				System.exit(0);

			}
		});
		mnNewMenu.add(mntmExit);

		helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);

		about = new JMenuItem("About Durak");
		about.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {

				About aboutScreen = new About();
				aboutScreen.setVisible(true);
			}
		});

		helpMenu.add(about);
		gamingTableView = new GamingTableView();
		gamingTableView.setToolTipText("Gaming Table");
		gamingTableView.setBorder(null);
		getContentPane().add(gamingTableView, BorderLayout.CENTER);

		handView = new HandView();
		handView.setToolTipText("Your Hand");
		handView.setBorder(null);
		getContentPane().add(handView, BorderLayout.SOUTH);

		scoreBoard = new DurakPanel();
		scoreBoard.setToolTipText("ScoreBoard");
		scoreBoard.setForeground(Color.WHITE);
		getContentPane().add(scoreBoard, BorderLayout.WEST);
		GridBagLayout gbl_scoreBoard = new GridBagLayout();
		gbl_scoreBoard.columnWidths = new int[] { 0, 0, 50 };
		gbl_scoreBoard.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50 };
		gbl_scoreBoard.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
		gbl_scoreBoard.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		scoreBoard.setLayout(gbl_scoreBoard);

		buttonPanel = new DurakPanel();
		buttonPanel.setToolTipText("Play Buttons");
		buttonPanel.setBorder(null);
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
		getContentPane().add(buttonPanel, BorderLayout.EAST);

		btnAttack = new JButton("   Attack!    ");
		btnAttack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		buttonPanel.add(btnAttack);
		btnAttack.setEnabled(false);

		buttonPanel.add(Box.createVerticalStrut(50));

		btnPassattack = new JButton("PassAttack");
		buttonPanel.add(btnPassattack);
		btnPassattack.setEnabled(false);

		buttonPanel.add(Box.createVerticalStrut(50));

		btnWithdraw = new JButton("  Withdraw ");
		buttonPanel.add(btnWithdraw);
		btnWithdraw.setEnabled(false);
	}

	/**
	 * 
	 */
	public void init() {

	}

	public HandView getHandView() {
		return handView;
	}

	public void showMessageFromServer(String message) {
		JLabel messageLabel = new JLabel(message);
		messageLabel.setForeground(Color.WHITE);
		messagesFromServer.removeAll();
		messagesFromServer.add(messageLabel);
		messagesFromServer.repaint();
	}

	public void enablePassAttackButton(boolean b) {
		btnPassattack.setEnabled(b);
	}

	public void enableAttackButton(boolean b) {
		btnAttack.setEnabled(b);
	}

	public void setWithdrawListener(ActionListener withdrawListener) {
		this.withdrawListener = withdrawListener;
		btnWithdraw.addActionListener(withdrawListener);
	}

	public void enableWithdrawButton(boolean b) {
		btnWithdraw.setEnabled(b);

	}

	public void updateScoreBoard(GameTableState gameState) {
		scoreBoard.removeAll();

		scoreBoardHeader = new JLabel("Score Board:");
		scoreBoardHeader.setForeground(Color.YELLOW);
		scoreBoardHeader.setFont(new Font("Segoe UI Semibold", Font.BOLD, 15));
		GridBagConstraints gbc_ScoreBoardHeader = new GridBagConstraints();
		gbc_ScoreBoardHeader.gridx = 0;
		gbc_ScoreBoardHeader.gridy = 2;
		scoreBoard.add(scoreBoardHeader, gbc_ScoreBoardHeader);

		int j = 3;

		for (CommonPlayerData player : gameState.getPlayers()) {
			int i = 0;
			playerNameLable = new JLabel("");
			playerNameLable.setForeground(Color.YELLOW);
			playerNameLable.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
			GridBagConstraints gbc_PlayerAnameLable = new GridBagConstraints();
			gbc_PlayerAnameLable.insets = new Insets(0, 0, 5, 5);
			gbc_PlayerAnameLable.gridx = i;
			gbc_PlayerAnameLable.gridy = j;
			playerNameLable.setText(player.getName() + ": ");
			scoreBoard.add(playerNameLable, gbc_PlayerAnameLable);
			i++;

			playerScoreLable = new JLabel("");
			playerScoreLable.setForeground(Color.YELLOW);
			GridBagConstraints gbc_PlayerAScoreLable = new GridBagConstraints();
			gbc_PlayerAScoreLable.insets = new Insets(0, 0, 5, 0);
			gbc_PlayerAScoreLable.gridx = i;
			gbc_PlayerAScoreLable.gridy = j;
			scoreBoard.add(playerScoreLable, gbc_PlayerAScoreLable);
			playerScoreLable.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 12));
			playerScoreLable.setText(Integer.toString(player.getLoses()));
			j++;
		}
		scoreBoard.repaint();
	}

}
